﻿using OnlineAuction.Business;
using OnlineAuction.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineAuction.Users
{
    public partial class ManageHome : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["UserInfo"] == null)
            {
                Response.Redirect("../Home.aspx");
            }
            else if (!Page.IsPostBack)
            {
                GetItemsSelling();
            }
        }

        protected void UpdateTimer_Tick(object sender, EventArgs e)
        {
            GetItemsSelling();
        }


        private void GetItemsSelling()
        {
            //Get all item selling by user logged in
            ItemBO bo = new ItemBO();
            List<ItemsE> list = bo.ItemsSelling(Request.Cookies["UserInfo"]["UserName"].ToString());
            cpItemsSelling.PageSize = 10;
            cpItemsSelling.ShowFirstLast = true;
            cpItemsSelling.DataSource = list;
            cpItemsSelling.BindToControl = grvItems;
            grvItems.DataSource = cpItemsSelling.DataSourcePaged;
        }

        protected void grvItems_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.CompareTo("Disable") == 0)
            {
                string[] arg = e.CommandArgument.ToString().Split('-');
                if (arg[1].CompareTo("auto") == 0)
                {
                    ScriptManager.RegisterStartupScript(this, GetType(), "alertmsg",
                        "alert('You can not disable this Item');", true);
                }
                else
                {
                    AuctionBO bo = new AuctionBO();
                    if (bo.FinishAuctionSessionImmediately(int.Parse(arg[0])))
                    {
                        Response.Redirect(Request.RawUrl);
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, GetType(), "alertmsg",
                        "alert('Fail to finish...!!!');", true);
                    }
                }
            }
        }
    }
}